package com.jdbc.text8;

import java.sql.ResultSet;
import java.sql.SQLException;

public class NewsDaoImp1 extends BaseDao implements NewsDao {

    //查看
    @Override
    public void getNewsByTitle() {
        try{
            String sql = "SELECT id,userName,password,email,userType FROM news_user";
            Object[] params ={};
            ResultSet re = this.executeSQL(sql ,params);
            while (rs.next()){
                int id=rs.getInt("id");

                String userName = rs.getString("userName");
                String password = rs.getString("password");
                String email = rs.getString("email");
                int userType=rs.getInt("userType");

                System.out.println(id +"\t"+userName +"\t"+password +"\t"+email +"\t"+userType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
   //增加
    @Override
    public void addNews(int id,String userName , String password,String email , int userType) {
        try {
            String sql="INSERT INTO news_user(id,userName,password,email,userType)VALUES(?,?,?,?,?) ";
            Object[] params ={id,userName,password,email,userType};
            int i = this.executeUpdate(sql,params);
            if (i>0){
                System.out.println("新增成功");
            }
        } finally {
            try{
                preparedStatement.close();
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }

        }
    }

    @Override
    public void deleteNews(int id) {
       try{
           this.getConnection();
           String sql = "DELETE FROM news_user WHERE id=?";
           preparedStatement = connection.prepareStatement(sql);
           preparedStatement.setInt(1,id);
           int i=preparedStatement.executeUpdate();
           if (i>0){
               System.out.println("删除成功");
           }
       }catch (SQLException e){
           e.printStackTrace();
       }

    }

    @Override
    public void updateNews(int id, String password) throws SQLException {
        try{
            String sql="UPDATE news_user SET password=?WHERE id=?";
            Object[] params ={password,id};
            int i=this.executeUpdate(sql,params);
            if (i>0){
                System.out.println("修改成功");
            }
        } finally {
            preparedStatement.close();
            connection.close();
        }


    }

}
